package com.trinity.orders.controller;

import com.github.binarywang.wxpay.exception.WxPayException;
import com.trinity.common.annotation.Log;
import com.trinity.common.core.controller.BaseController;
import com.trinity.common.core.domain.AjaxResult;
import com.trinity.common.core.page.TableDataInfo;
import com.trinity.common.enums.BusinessType;
import com.trinity.common.utils.SecurityUtils;
import com.trinity.common.utils.poi.ExcelUtil;
import com.trinity.orders.domain.OderStudents;
import com.trinity.orders.domain.XcOrders;
import com.trinity.orders.dto.AddManualOrderDto;
import com.trinity.orders.dto.AddOrderDto;
import com.trinity.orders.dto.PayRecordDto;
import com.trinity.orders.dto.QueryXcOrdersDto;
import com.trinity.orders.service.IXcOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单信息Controller
 *
 * @author trinity
 * @date 2023-11-19
 */
@Api(value = "订单支付接口", tags = "订单支付接口")
@Slf4j
@RestController
@RequestMapping("/order/orders")
public class XcOrdersController extends BaseController {
    @Resource
    private IXcOrdersService xcOrdersService;

    /**
     * 查询订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(QueryXcOrdersDto xcOrders) {
        startPage();
        OderStudents student = new OderStudents();
        student.setStuNo(xcOrders.getStuNo());
        student.setStudentName(xcOrders.getStudentName());
        xcOrders.setStudent(student);
        List<XcOrders> list = xcOrdersService.selectXcOrdersList(xcOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:orders:export')")
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XcOrders xcOrders) {
        List<XcOrders> list = xcOrdersService.selectXcOrdersList(xcOrders);
        ExcelUtil<XcOrders> util = new ExcelUtil<>(XcOrders.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(xcOrdersService.selectXcOrdersById(id));
    }


    @ApiOperation("生成支付二维码")
    @Log(title = "下单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
    @RequestMapping(value = "/generatepaycode", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult generatePayCode(@RequestBody AddOrderDto addOrderDto) {
        //获取用户id
        String userId = SecurityUtils.getUserId().toString();

        //调用service 完成插入订单信息 插入支付记录 生成支付二维码
        PayRecordDto order = xcOrdersService.createOrderAndPayRecord(userId, addOrderDto);

        return success(order);

    }

    @ApiOperation("手工单")
    @Log(title = "手工单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
    @RequestMapping(value = "/manualOrderId", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult manualOrder(@RequestBody AddManualOrderDto addOrderDto) {
        //获取用户id
        String userId = SecurityUtils.getUserId().toString();

        //调用service 完成插入订单信息 插入支付记录 生成支付二维码
        PayRecordDto order = xcOrdersService.createManualOrder(userId, addOrderDto);

        return success(order);

    }

    @ApiOperation("小程序下单")
    @Log(title = "下单", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('order:orders:add')")
    @RequestMapping(value = "/createOrder", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult createOrder(@RequestBody AddOrderDto addOrderDto) throws WxPayException {
        //获取用户id
        String userId = SecurityUtils.getUserId().toString();

        //调用service 完成插入订单信息 插入支付记录 生成支付二维码
        PayRecordDto order = xcOrdersService.AppcreateOrder(userId, addOrderDto);

        return success(order);

    }


    /**
     * 修改订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:orders:edit')")
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody XcOrders xcOrders) {
        return toAjax(xcOrdersService.updateXcOrders(xcOrders));
    }

    /**
     * 删除订单信息
     */
    @PreAuthorize("@ss.hasPermi('order:orders:remove')")
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}/delete")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(xcOrdersService.deleteXcOrdersByIds(ids));
    }
}
